package com.youyi.yyviewsdklibrary.picker.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;
import com.lkx.liandianqinew.R;
import com.youyi.yyviewsdklibrary.picker.contract.DateFormatter;
import com.youyi.yyviewsdklibrary.picker.contract.OnDateSelectedListener;
import com.youyi.yyviewsdklibrary.picker.contract.WheelFormatter;
import com.youyi.yyviewsdklibrary.picker.entity.DateEntity;
import com.youyi.yyviewsdklibrary.picker.impl.UnitDateFormatter;
import java.util.Arrays;
import java.util.List;

public class MonthWheelLayout extends BaseWheelLayout {
    private TextView dayLabelView;
    private NumberWheelView dayWheelView;
    private DateEntity endValue;
    private TextView monthLabelView;
    private NumberWheelView monthWheelView;
    private OnDateSelectedListener onDateSelectedListener;
    private boolean resetWhenLinkage = true;
    private Integer selectedDay;
    private Integer selectedMonth;
    private Integer selectedYear;
    private DateEntity startValue;
    private TextView yearLabelView;
    private NumberWheelView yearWheelView;

    public MonthWheelLayout(Context context) {
        super(context);
    }

    public MonthWheelLayout(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
    }

    public MonthWheelLayout(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
    }

    public MonthWheelLayout(Context context, AttributeSet attributeSet, int i, int i2) {
        super(context, attributeSet, i, i2);
    }

    /* access modifiers changed from: protected */
    @Override // com.youyi.yyviewsdklibrary.picker.widget.BaseWheelLayout
    public int provideLayoutRes() {
        return R.layout.wheel_picker_month;
    }

    /* access modifiers changed from: protected */
    @Override // com.youyi.yyviewsdklibrary.picker.widget.BaseWheelLayout
    public List<NumberWheelView> provideWheelViews() {
        return Arrays.asList(this.yearWheelView, this.monthWheelView, this.dayWheelView);
    }

    /* access modifiers changed from: protected */
    @Override // com.youyi.yyviewsdklibrary.picker.widget.BaseWheelLayout
    public void onInit(Context context) {
        this.yearWheelView = (NumberWheelView) findViewById(R.id.wheel_picker_date_year_wheel);
        this.monthWheelView = (NumberWheelView) findViewById(R.id.wheel_picker_date_month_wheel);
        this.dayWheelView = (NumberWheelView) findViewById(R.id.wheel_picker_date_day_wheel);
        this.yearLabelView = (TextView) findViewById(R.id.wheel_picker_date_year_label);
        this.monthLabelView = (TextView) findViewById(R.id.wheel_picker_date_month_label);
        this.dayLabelView = (TextView) findViewById(R.id.wheel_picker_date_day_label);
    }

    /* access modifiers changed from: protected */
    @Override // com.youyi.yyviewsdklibrary.picker.widget.BaseWheelLayout
    public void onAttributeSet(Context context, AttributeSet attributeSet) {
        TypedArray obtainStyledAttributes = context.obtainStyledAttributes(attributeSet, R.styleable.DateWheelLayout);
        setDateMode(obtainStyledAttributes.getInt(R.styleable.DateWheelLayout_wheel_dateMode, 2));
        String string = obtainStyledAttributes.getString(R.styleable.DateWheelLayout_wheel_yearLabel);
        String string2 = obtainStyledAttributes.getString(R.styleable.DateWheelLayout_wheel_monthLabel);
        String string3 = obtainStyledAttributes.getString(R.styleable.DateWheelLayout_wheel_dayLabel);
        boolean z = obtainStyledAttributes.getBoolean(R.styleable.DateWheelLayout_is_old, false);
        obtainStyledAttributes.recycle();
        setDateLabel(string, string2, string3);
        setDateFormatter(new UnitDateFormatter(z));
    }

    public void setIsOld(boolean z) {
        setDateFormatter(new UnitDateFormatter(z));
    }

    /* access modifiers changed from: protected */
    public void onVisibilityChanged(View view, int i) {
        super.onVisibilityChanged(view, i);
        if (i == 0 && this.startValue == null && this.endValue == null) {
            setRange(DateEntity.today(), DateEntity.yearOnFuture(30), DateEntity.today());
        }
    }

    @Override // com.youyi.yyviewsdklibrary.picker.contract.OnWheelChangedListener
    public void onWheelSelected(WheelView wheelView, int i) {
        int id = wheelView.getId();
        if (id == R.id.wheel_picker_date_year_wheel) {
            Integer num = (Integer) this.yearWheelView.getItem(i);
            this.selectedYear = num;
            if (this.resetWhenLinkage) {
                this.selectedMonth = null;
                this.selectedDay = null;
            }
            changeMonth(num.intValue());
            dateSelectedCallback();
        } else if (id == R.id.wheel_picker_date_month_wheel) {
            this.selectedMonth = (Integer) this.monthWheelView.getItem(i);
            if (this.resetWhenLinkage) {
                this.selectedDay = null;
            }
            changeDay(this.selectedYear.intValue(), this.selectedMonth.intValue());
            dateSelectedCallback();
        } else if (id == R.id.wheel_picker_date_day_wheel) {
            this.selectedDay = (Integer) this.dayWheelView.getItem(i);
            dateSelectedCallback();
        }
    }

    @Override // com.youyi.yyviewsdklibrary.picker.contract.OnWheelChangedListener, com.youyi.yyviewsdklibrary.picker.widget.BaseWheelLayout
    public void onWheelScrollStateChanged(WheelView wheelView, int i) {
        int id = wheelView.getId();
        boolean z = true;
        if (id == R.id.wheel_picker_date_year_wheel) {
            this.monthWheelView.setEnabled(i == 0);
            NumberWheelView numberWheelView = this.dayWheelView;
            if (i != 0) {
                z = false;
            }
            numberWheelView.setEnabled(z);
        } else if (id == R.id.wheel_picker_date_month_wheel) {
            this.yearWheelView.setEnabled(i == 0);
            NumberWheelView numberWheelView2 = this.dayWheelView;
            if (i != 0) {
                z = false;
            }
            numberWheelView2.setEnabled(z);
        } else if (id == R.id.wheel_picker_date_day_wheel) {
            this.yearWheelView.setEnabled(i == 0);
            NumberWheelView numberWheelView3 = this.monthWheelView;
            if (i != 0) {
                z = false;
            }
            numberWheelView3.setEnabled(z);
        }
    }

    private void dateSelectedCallback() {
        if (this.onDateSelectedListener != null) {
            this.dayWheelView.post(new Runnable() {
                /* class com.youyi.yyviewsdklibrary.picker.widget.MonthWheelLayout.AnonymousClass1 */

                public void run() {
                    MonthWheelLayout.this.onDateSelectedListener.onDateSelected(MonthWheelLayout.this.selectedYear.intValue(), MonthWheelLayout.this.selectedMonth.intValue(), MonthWheelLayout.this.selectedDay.intValue());
                }
            });
        }
    }

    public void setDateMode(int i) {
        this.yearWheelView.setVisibility(View.VISIBLE);
        this.yearLabelView.setVisibility(View.VISIBLE);
        this.monthWheelView.setVisibility(View.VISIBLE);
        this.monthLabelView.setVisibility(View.VISIBLE);
        this.dayWheelView.setVisibility(View.VISIBLE);
        this.dayLabelView.setVisibility(View.VISIBLE);
        if (i == -1) {
            this.yearWheelView.setVisibility(View.GONE);
            this.yearLabelView.setVisibility(View.GONE);
            this.monthWheelView.setVisibility(View.GONE);
            this.monthLabelView.setVisibility(View.GONE);
            this.dayWheelView.setVisibility(View.GONE);
            this.dayLabelView.setVisibility(View.GONE);
        } else if (i == 2) {
            this.yearWheelView.setVisibility(View.GONE);
            this.yearLabelView.setVisibility(View.GONE);
        } else if (i == 1) {
            this.dayWheelView.setVisibility(View.GONE);
            this.dayLabelView.setVisibility(View.GONE);
        }
    }

    public void setRange(DateEntity dateEntity, DateEntity dateEntity2) {
        setRange(dateEntity, dateEntity2, null);
    }

    public void setRange(DateEntity dateEntity, DateEntity dateEntity2, DateEntity dateEntity3) {
        if (dateEntity == null) {
            dateEntity = DateEntity.today();
        }
        if (dateEntity2 == null) {
            dateEntity2 = DateEntity.yearOnFuture(30);
        }
        if (dateEntity2.toTimeInMillis() >= dateEntity.toTimeInMillis()) {
            this.startValue = dateEntity;
            this.endValue = dateEntity2;
            if (dateEntity3 != null) {
                this.selectedYear = Integer.valueOf(dateEntity3.getYear());
                this.selectedMonth = Integer.valueOf(dateEntity3.getMonth());
                this.selectedDay = Integer.valueOf(dateEntity3.getDay());
            } else {
                this.selectedYear = null;
                this.selectedMonth = null;
                this.selectedDay = null;
            }
            changeYear();
            return;
        }
        throw new IllegalArgumentException("Ensure the start date is less than the end date");
    }

    public void setDefaultValue(DateEntity dateEntity) {
        setRange(this.startValue, this.endValue, dateEntity);
    }

    public void setDateFormatter(final DateFormatter dateFormatter) {
        if (dateFormatter != null) {
            this.yearWheelView.setFormatter(new WheelFormatter() {
                /* class com.youyi.yyviewsdklibrary.picker.widget.MonthWheelLayout.AnonymousClass2 */

                @Override // com.youyi.yyviewsdklibrary.picker.contract.WheelFormatter
                public String formatItem(Object obj) {
                    return dateFormatter.formatYear(((Integer) obj).intValue());
                }
            });
            this.monthWheelView.setFormatter(new WheelFormatter() {
                /* class com.youyi.yyviewsdklibrary.picker.widget.MonthWheelLayout.AnonymousClass3 */

                @Override // com.youyi.yyviewsdklibrary.picker.contract.WheelFormatter
                public String formatItem(Object obj) {
                    return dateFormatter.formatMonth(((Integer) obj).intValue());
                }
            });
            this.dayWheelView.setFormatter(new WheelFormatter() {
                /* class com.youyi.yyviewsdklibrary.picker.widget.MonthWheelLayout.AnonymousClass4 */

                @Override // com.youyi.yyviewsdklibrary.picker.contract.WheelFormatter
                public String formatItem(Object obj) {
                    return dateFormatter.formatDay(((Integer) obj).intValue());
                }
            });
        }
    }

    public void setDateLabel(CharSequence charSequence, CharSequence charSequence2, CharSequence charSequence3) {
        this.yearLabelView.setText(charSequence);
        this.monthLabelView.setText(charSequence2);
        this.dayLabelView.setText(charSequence3);
    }

    public void setOnDateSelectedListener(OnDateSelectedListener onDateSelectedListener2) {
        this.onDateSelectedListener = onDateSelectedListener2;
    }

    public void setResetWhenLinkage(boolean z) {
        this.resetWhenLinkage = z;
    }

    public final DateEntity getStartValue() {
        return this.startValue;
    }

    public final DateEntity getEndValue() {
        return this.endValue;
    }

    public final NumberWheelView getYearWheelView() {
        return this.yearWheelView;
    }

    public final NumberWheelView getMonthWheelView() {
        return this.monthWheelView;
    }

    public final NumberWheelView getDayWheelView() {
        return this.dayWheelView;
    }

    public final TextView getYearLabelView() {
        return this.yearLabelView;
    }

    public final TextView getMonthLabelView() {
        return this.monthLabelView;
    }

    public final TextView getDayLabelView() {
        return this.dayLabelView;
    }

    public final int getSelectedYear() {
        return ((Integer) this.yearWheelView.getCurrentItem()).intValue();
    }

    public final int getSelectedMonth() {
        return ((Integer) this.monthWheelView.getCurrentItem()).intValue();
    }

    public final int getSelectedDay() {
        return ((Integer) this.dayWheelView.getCurrentItem()).intValue();
    }

    private void changeYear() {
        int min = Math.min(this.startValue.getYear(), this.endValue.getYear());
        int max = Math.max(this.startValue.getYear(), this.endValue.getYear());
        Integer num = this.selectedYear;
        if (num == null) {
            this.selectedYear = Integer.valueOf(min);
        } else {
            Integer valueOf = Integer.valueOf(Math.max(num.intValue(), min));
            this.selectedYear = valueOf;
            this.selectedYear = Integer.valueOf(Math.min(valueOf.intValue(), max));
        }
        this.yearWheelView.setRange(min, max, 1);
        this.yearWheelView.setDefaultValue(this.selectedYear);
        changeMonth(this.selectedYear.intValue());
    }

    private void changeMonth(int i) {
        int i2;
        int i3 = 12;
        if (this.startValue.getYear() == this.endValue.getYear()) {
            i2 = Math.min(this.startValue.getMonth(), this.endValue.getMonth());
            i3 = Math.max(this.startValue.getMonth(), this.endValue.getMonth());
        } else if (i == this.startValue.getYear()) {
            i2 = this.startValue.getMonth();
        } else {
            if (i == this.endValue.getYear()) {
                i3 = this.endValue.getMonth();
            }
            i2 = 1;
        }
        Integer num = this.selectedMonth;
        if (num == null) {
            this.selectedMonth = Integer.valueOf(i2);
        } else {
            Integer valueOf = Integer.valueOf(Math.max(num.intValue(), i2));
            this.selectedMonth = valueOf;
            this.selectedMonth = Integer.valueOf(Math.min(valueOf.intValue(), i3));
        }
        this.monthWheelView.setRange(i2, i3, 1);
        this.monthWheelView.setDefaultValue(this.selectedMonth);
        changeDay(i, this.selectedMonth.intValue());
    }

    private void changeDay(int i, int i2) {
        int i3;
        int i4;
        if (i == this.startValue.getYear() && i2 == this.startValue.getMonth() && i == this.endValue.getYear() && i2 == this.endValue.getMonth()) {
            i4 = this.startValue.getDay();
            i3 = this.endValue.getDay();
        } else if (i == this.startValue.getYear() && i2 == this.startValue.getMonth()) {
            int day = this.startValue.getDay();
            i3 = getTotalDaysInMonth(i, i2);
            i4 = day;
        } else {
            if (i == this.endValue.getYear() && i2 == this.endValue.getMonth()) {
                i3 = this.endValue.getDay();
            } else {
                i3 = getTotalDaysInMonth(i, i2);
            }
            i4 = 1;
        }
        Integer num = this.selectedDay;
        if (num == null) {
            this.selectedDay = Integer.valueOf(i4);
        } else {
            Integer valueOf = Integer.valueOf(Math.max(num.intValue(), i4));
            this.selectedDay = valueOf;
            this.selectedDay = Integer.valueOf(Math.min(valueOf.intValue(), i3));
        }
        this.dayWheelView.setRange(i4, i3, 1);
        this.dayWheelView.setDefaultValue(this.selectedDay);
    }

    private int getTotalDaysInMonth(int i, int i2) {
        boolean z = true;
        if (i2 == 1) {
            return 31;
        }
        if (i2 != 2) {
            return (i2 == 3 || i2 == 5 || i2 == 10 || i2 == 12 || i2 == 7 || i2 == 8) ? 31 : 30;
        }
        if (i <= 0) {
            return 29;
        }
        if ((i % 4 != 0 || i % 100 == 0) && i % 400 != 0) {
            z = false;
        }
        return z ? 29 : 28;
    }
}
